﻿using System;
using Gumini.JulaDB.Provider;
using Gumini.JulaDB.Test.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Gumini.JulaDB.Test
{
    /// <summary>
    /// A set of tests for checking scalar function results for given parameters.
    /// </summary>
    [TestClass]
    public class ScalarFunctionTest
    {
        private static JulaDbConnection _connection;

        [ClassInitialize]
        public static void InitializeDatabase(TestContext context)
        {
            _connection = TestUtils.CreateTestDatabase();
        }

        private void TestScalarFunction(string functionName, string input, object expectedOutput)
        {
            var result = new JulaDbCommand(String.Format("select {0}({1}) from dual", functionName, input), _connection).ExecuteScalar();
            Assert.AreEqual(expectedOutput, result);
        }

        /// <summary>
        /// Test for lower() function converting a string to lowercase.
        /// </summary>
        [TestMethod]
        public void Lower()
        {
            TestScalarFunction("lower", "'JulaDB'", "juladb");
        }

        /// <summary>
        /// Test for upper() function converting a string to uppercase.
        /// </summary>
        [TestMethod]
        public void Upper()
        {
            TestScalarFunction("upper", "'JulaDB'", "JULADB");
        }

        /// <summary>
        /// Test for abs() function getting an absolute value of a number.
        /// </summary>
        [TestMethod]
        public void Abs()
        {
            TestScalarFunction("abs", "34.2", 34.2M);
        }

        /// <summary>
        /// Test for abs() function getting an absolute value of a number.
        /// </summary>
        [TestMethod]
        public void Abs2()
        {
            TestScalarFunction("abs", "-56.12", 56.12M);
        }

        /// <summary>
        /// Test for power() function raising a number to a given power.
        /// </summary>
        [TestMethod]
        public void Power()
        {
            TestScalarFunction("power", "2, 8", 256M);
        }

        /// <summary>
        /// Test for len() function getting the number of characters within a varchar.
        /// </summary>
        [TestMethod]
        public void Len()
        {
            TestScalarFunction("len", "'mince pie'", 9M);
        }
    }
}
